using System.Drawing;
using JetBrains.CommonControls;
using JetBrains.ReSharper.Psi;
using JetBrains.ReSharper.TreeModelBrowser;
using JetBrains.ReSharper.UnitTestExplorer;
using JetBrains.UI.TreeView;
using JetBrains.Util.DataStructures.TreeModel;

namespace JamesKovacs.ReSharper.UnitTestSupport.VstsUnit {
    public class VstsUnitTestPresenter : TreeModelBrowserPresenter {
        public VstsUnitTestPresenter() {
            Present<VstsUnitTestFixtureElement>(PresentTestFixture);
            Present<VstsUnitTestElement>(PresentTest);
        }

        protected override bool IsNaturalParent(object parentValue, object childValue) {
            UnitTestNamespace ns = parentValue as UnitTestNamespace;
            VstsUnitTestFixtureElement fixture = childValue as VstsUnitTestFixtureElement;
            if((fixture != null) && (ns != null)) {
                return ns.Equals(fixture.GetNamespace());
            }
            return base.IsNaturalParent(parentValue, childValue);
        }

        protected virtual void PresentTest(VstsUnitTestElement value, IPresentableItem item, TreeModelNode modelNode, PresentationState state) {
            item.Clear();
            if(value.Fixture.GetTypeClrName() != value.GetTypeClrName()) {
                CLRTypeName clrTypeName = new CLRTypeName(value.GetTypeClrName());
                item.RichText = string.Format("{0}.{1}", clrTypeName.ShortName, value.MethodName);
            } else {
                item.RichText = value.MethodName;
            }
            if(value.IsExplicit) {
                item.RichText.SetForeColor(SystemColors.GrayText);
            }
            Image typeImage = UnitTestManager.GetStandardImage(UnitTestElementImage.Test);
            Image stateImage = UnitTestManager.GetStateImage(state);
            if(stateImage != null) {
                item.Images.Add(stateImage);
            } else if(typeImage != null) {
                item.Images.Add(typeImage);
            }
        }

        protected virtual void PresentTestFixture(VstsUnitTestFixtureElement value, IPresentableItem item, TreeModelNode modelNode, PresentationState state) {
            item.Clear();
            if(IsNodeParentNatural(modelNode, value)) {
                CLRTypeName clrTypeName = new CLRTypeName(value.GetTypeClrName());
                item.RichText = clrTypeName.ShortName;
            } else {
                CLRTypeName name = new CLRTypeName(value.GetTypeClrName());
                if(string.IsNullOrEmpty(name.NamespaceName)) {
                    item.RichText = string.Format("{0}", name.ShortName);
                } else {
                    item.RichText = string.Format("{0}.{1}", name.NamespaceName, name.ShortName);
                }
            }
            Image typeImage = UnitTestManager.GetStandardImage(UnitTestElementImage.TestContainer);
            Image stateImage = UnitTestManager.GetStateImage(state);
            if(stateImage != null) {
                item.Images.Add(stateImage);
            } else if(typeImage != null) {
                item.Images.Add(typeImage);
            }
            AppendOccurencesCount(item, modelNode, "test");
        }

        protected override object Unwrap(object value) {
            VstsUnitTestElement testElement = value as VstsUnitTestElement;
            if(testElement != null) {
                value = testElement.GetDeclaredElement();
            }
            VstsUnitTestFixtureElement testFixture = value as VstsUnitTestFixtureElement;
            if(testFixture != null) {
                value = testFixture.GetDeclaredElement();
            }
            return base.Unwrap(value);
        }
    }
}